<?php

/**
 * Question form base class.
 *
 * @package    elearning
 * @subpackage form
 * @author     Your name here
 */
class BaseQuestionForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                 => new sfWidgetFormInputHidden(),
      'level_id'           => new sfWidgetFormPropelChoice(array('model' => 'Level', 'add_empty' => false)),
      'teacher_id'         => new sfWidgetFormPropelChoice(array('model' => 'Teacher', 'add_empty' => false)),
      'question_type_id'   => new sfWidgetFormPropelChoice(array('model' => 'QuestionType', 'add_empty' => false)),
      'question_subject'   => new sfWidgetFormInput(),
      'question_content'   => new sfWidgetFormTextarea(),
      'mark'               => new sfWidgetFormInput(),
      'question_status'    => new sfWidgetFormInput(),
      'created_at'         => new sfWidgetFormDateTime(),
      'updated_at'         => new sfWidgetFormDateTime(),
      'question_test_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Test')),
    ));

    $this->setValidators(array(
      'id'                 => new sfValidatorPropelChoice(array('model' => 'Question', 'column' => 'id', 'required' => false)),
      'level_id'           => new sfValidatorPropelChoice(array('model' => 'Level', 'column' => 'id')),
      'teacher_id'         => new sfValidatorPropelChoice(array('model' => 'Teacher', 'column' => 'id')),
      'question_type_id'   => new sfValidatorPropelChoice(array('model' => 'QuestionType', 'column' => 'id')),
      'question_subject'   => new sfValidatorString(array('max_length' => 255)),
      'question_content'   => new sfValidatorString(),
      'mark'               => new sfValidatorNumber(),
      'question_status'    => new sfValidatorInteger(),
      'created_at'         => new sfValidatorDateTime(array('required' => false)),
      'updated_at'         => new sfValidatorDateTime(array('required' => false)),
      'question_test_list' => new sfValidatorPropelChoiceMany(array('model' => 'Test', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('question[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Question';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['question_test_list']))
    {
      $values = array();
      foreach ($this->object->getQuestionTests() as $obj)
      {
        $values[] = $obj->getTestId();
      }

      $this->setDefault('question_test_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveQuestionTestList($con);
  }

  public function saveQuestionTestList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['question_test_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(QuestionTestPeer::QUESTION_ID, $this->object->getPrimaryKey());
    QuestionTestPeer::doDelete($c, $con);

    $values = $this->getValue('question_test_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new QuestionTest();
        $obj->setQuestionId($this->object->getPrimaryKey());
        $obj->setTestId($value);
        $obj->save();
      }
    }
  }

}
